package com.emellend.api11.os;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.telephony.SmsMessage;

public class SmsMessageReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle extras = intent.getExtras();
		if (extras==null) {
			return;
		}
		Object[] pdus=(Object[]) extras.get("pdus");
		for (int i = 0; i < pdus.length; i++) {
			byte[] pdu=(byte[]) pdus[i];
			SmsMessage message = SmsMessage.createFromPdu(pdu);
			String fromAddress = message.getOriginatingAddress();
			String fromDisplayName=fromAddress;
			
			Uri uri;
			String[] projection;
			uri=Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(fromAddress));
			projection=new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME};
			Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
			if (cursor!=null) {
				if (cursor.moveToFirst()) {
					fromDisplayName=cursor.getString(0);
				}
				cursor.close();
			}
			Intent di=new Intent();
			di.setClass(context, SmsReceivedDialog.class);
			di.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
			di.putExtra(SmsReceivedDialog.SMS_FROM_ADDRESS_EXTRA, fromAddress);
			di.putExtra(SmsReceivedDialog.SMS_FROM_DISPLAY_NAME_EXTRA, fromDisplayName);
			di.putExtra(SmsReceivedDialog.SMS_MESSAGE_EXTRA, message.getMessageBody().toString());
			context.startActivity(di);
			break;
		}
	}

}
